package com.sd.trans.controller;

import com.sd.trans.entity.TransRec;
import com.sd.trans.service.TransRecService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (TransRec)表控制层
 *
 * @author teacherChen
 * @since 2025-11-04 11:12:34
 */
@RestController
@RequestMapping("transRec")
public class TransRecController {
    /**
     * 服务对象
     */
    @Resource
    private TransRecService transRecService;

    /**
     * 分页查询
     *
     * @param transRec 筛选条件
     *                 page 当前页码
     *                 size 每页大小
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<TransRec>> queryByPage(TransRec transRec,Integer  page,Integer size ) {
        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC,"id"));
        return ResponseEntity.ok(this.transRecService.queryByPage(transRec, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TransRec> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.transRecService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param transRec 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<TransRec> add(@RequestBody TransRec transRec) {
        return ResponseEntity.ok(this.transRecService.insert(transRec));
    }

    /**
     * 编辑数据
     *
     * @param transRec 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TransRec> edit(@RequestBody TransRec transRec) {
        return ResponseEntity.ok(this.transRecService.update(transRec));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.transRecService.deleteById(id));
    }

}

